package com.yj.lottery_system.controller.handler;

import com.yj.lottery_system.common.errorcode.GlobalErrorCodeConstants;
import com.yj.lottery_system.common.exception.ControllerException;
import com.yj.lottery_system.common.exception.ServiceException;
import com.yj.lottery_system.common.pojo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice //捕获全局抛的异常

public class GlobalExceptionHandler {
    private final static Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<?> serviceException(ServiceException e) {
        //错误日志
        LOGGER.error("ServiceException: ", e);
        //构造返回结果
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVICE_ERROR.getCode(), e.getMessage());
    }
    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<?> controllerException(ControllerException e) {
        //错误日志
        LOGGER.error("ControllerException: ", e);
        //构造返回结果
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVICE_ERROR.getCode(), e.getMessage());
    }
    @ExceptionHandler(value = Exception.class)
    public CommonResult<?> exception(Exception e) {
        //错误日志
        LOGGER.error("ControllerException: ", e);
        //构造返回结果
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVICE_ERROR.getCode(), e.getMessage());
    }

}
